#!/bin/bash

# Project Calico BPF dataplane build scripts.
# Copyright (c) 2020 Tigera, Inc. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Generate filenames of UT-only programs; in particular ones that force-set the packet mark to
# some known values.
#
# WARNING: should be kept in sync with the combinations used in tests, in particular bpf_prog_test.go.

emit_filename() {
  echo "bin/test_${from_or_to}_${ep_type}_fib_${log_level}_skb${skb}${dsr}.o"
}

((mark_calico = 0xc0000000))
((mark_seen = mark_calico | 0x1000000))
((mark_seen_bypass = mark_seen | 0x2000000))
((mark_seen_bypass_src_fixup = mark_seen_bypass | 0x500000))
((mark_seen_bypass_forward = mark_seen_bypass | 0x300000))
((mark_seen_bypass_skip_rpf = mark_seen_bypass | 0x400000))

log_level=debug
ep_types="wep hep"
for ep_type in $ep_types; do
  directions="from to"
  for from_or_to in $directions; do
    for skb in "0x0" \
      $(printf 0x%x $mark_seen) \
      $(printf 0x%x $mark_seen_bypass_src_fixup) \
      $(printf 0x%x $mark_seen_bypass_skip_rpf) \
      $(printf 0x%x $mark_seen_bypass_forward); do
      for dsr in "" "_dsr"; do
        if [ "${dsr}" = "_dsr" ]; then
          if [ "${ep_type}" = "hep" ]; then
            emit_filename
            continue
          fi
          if [ "${from_or_to}" = "from" ] && [ "${ep_type}" = "wep" ]; then
            emit_filename
            continue
          fi
        else
          emit_filename
        fi
      done
    done
  done
done
